package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.SystemMessage;
import cn.com.connext.scrm.domain.master.SystemMessageUserLog;
import cn.com.connext.scrm.service.SystemMessageService;
import cn.com.connext.scrm.service.SystemMessageUserLogService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.dto.SystemMessageDTO;
import cn.com.connext.scrm.service.dto.SystemMessageUserLogDTO;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.vm.PageQueryVM;
import cn.com.connext.scrm.web.rest.vm.QueryIdVM;
import cn.com.connext.scrm.web.rest.vm.SystemMessageVM;
import cn.com.connext.scrm.web.rest.vm.UserIdVM;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.exception.ServiceException;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: organization-service
 * @author: shawn
 * @create: 2019-06-03 10:48
 **/
@RestController
@RequestMapping("/api/systemMessage/")
public class SystemMessageResource extends BaseResource {
    public SystemMessageResource() {
        super("systemMessage");
    }

    @Autowired
    private SystemMessageService systemMessageService;

    @Autowired
    private SystemMessageUserLogService systemMessageUserLogService;

    private final Logger log = LoggerFactory.getLogger(SystemMessageResource.class);


    @PostMapping(value = "list")
    @Timed
    @ApiOperation(value = "系统消息列表", tags = {"系统消息管理"}, notes = "系统消息列表")
    public ResponseEntity list (@RequestBody PageQueryVM vm){
        JsonResult result = new JsonResult();
        try{
            Page<SystemMessage> page = systemMessageService.pageQuery(vm);
            return pagedResponse(page);
        }catch (Exception e){
            log.error("查询错误:{}", e);
            result.setMessage("查询错误");
            result.setState(1);
            return entityResponse(result);
        }
    }

    @PostMapping(value = "detail")
    @Timed
    @ApiOperation(value = "系统消息详情", tags = {"系统消息管理"}, notes = "系统消息详情")
    public ResponseEntity detail (@RequestBody QueryIdVM vm){
        JsonResult result = new JsonResult();
        try{
            if (vm == null){
                result.setMessage("参数错误");
                result.setState(1);
                return entityResponse(result);
            }
            SystemMessageDTO dto = systemMessageService.findById(vm.getId());
            if (dto == null){
                result.setState(1);
                result.setMessage("数据不存在");
                return entityResponse(result);
            }
            result.setState(0);
            result.setMessage("查询成功");
            result.setData(dto);
            return entityResponse(result);
        }catch (Exception e){
            log.error("查询详情出错:{}", e);
            result.setState(1);
            result.setMessage("查询错误");
            return entityResponse(result);
        }
    }

    @PostMapping(value = "edit")
    @Timed
    @ApiOperation(value = "编辑系统消息", tags = {"系统消息管理"}, notes = "编辑系统消息")
    public ResponseEntity edit(@RequestBody SystemMessageVM vm){
        JsonResult result = new JsonResult();
        try{
            SystemMessageDTO dto = systemMessageService.edit(vm);
            result.setMessage("编辑成功");
            result.setState(0);
            return entityResponse(result);
        }catch (Exception e){
            log.error("编辑错误:{}", e);
            result.setState(1);
            result.setMessage("编辑错误");
            return entityResponse(result);
        }
    }

    @PostMapping("delete")
    @Timed
    @ApiOperation(value = "删除系统消息", tags = {"系统消息管理"}, notes = "删除系统消息")
    public ResponseEntity delete(@RequestBody QueryIdVM vm){
        JsonResult result = new JsonResult();
        try{
            result = systemMessageService.deleteById(vm.getId());
            return entityResponse(result);
        }catch (Exception e){
            log.error("删除失败:{}", e);
            result.setMessage("删除失败");
            result.setState(1);
            return entityResponse(result);
        }
    }

    @PostMapping(value = "send")
    @Timed
    @ApiOperation(value = "发送系统消息", tags = {"系统消息管理"}, notes = "发送系统消息")
    public ResponseEntity send(@RequestBody QueryIdVM vm){
        JsonResult result = new JsonResult();
        try{
            if (vm == null){
                result.setState(1);
                result.setMessage("参数缺失");
                return entityResponse(result);
            }
            SystemMessageDTO dto = systemMessageService.send(vm.getId());
            if (dto == null){
                result.setMessage("数据异常");
                result.setState(1);
                return entityResponse(result);
            }
            result.setState(0);
            result.setMessage("发送成功");
            return entityResponse(result);
        }catch (Exception e){
            log.error("发送失败:{}", e);
            result.setMessage("发送失败");
            result.setState(1);
            return entityResponse(result);
        }
    }

    @PostMapping(value = "findCountByUserId")
    @Timed
    @ApiOperation(value = "根据userId查询系统消息数量", tags = {"用户记录"}, notes = "根据userId查询系统消息数量")
    public ResponseEntity findCountByUserId(@RequestBody UserIdVM vm){
        try{
            if (vm == null){
                throw new ServiceException(MyErrorCode.ERROR_USERID_EMPTY);
            }
            Integer num = systemMessageUserLogService.findCountByUserId(vm.getUserId());
            return entityResponse(num);
        }catch (Exception e){
            log.error("查询失败:{}", e);
            return null;
        }
    }

    @PostMapping(value = "findLogByUserId")
    @Timed
    @ApiOperation(value = "根据userId查询我的信息", tags = {"用户记录"}, notes = "根据userId查询我的信息")
    public ResponseEntity findByUserId(@RequestBody UserIdVM vm){
        try{
            if (vm == null){
                throw new ServiceException(MyErrorCode.ERROR_USERID_EMPTY);
            }
            Page<SystemMessageUserLogDTO> page = systemMessageUserLogService.findAllByUserId(vm);
            return pagedResponse(page);
        }catch (Exception e){
            log.error("查询失败:{}", e);
            return null;
        }
    }

    @PostMapping(value = "findLogDetail")
    @Timed
    @ApiOperation(value = "根据Id查询我的信息详情", tags = {"用户记录"}, notes = "根据Id查询我的信息详情")
    public ResponseEntity findLogDetail(@RequestBody QueryIdVM vm){
        try{
            if (vm == null){
                throw new ServiceException(MyErrorCode.ERROR_ID_EMPTY);
            }
            SystemMessageUserLog log = systemMessageUserLogService.findById(vm.getId());
            SystemMessageDTO message = systemMessageService.findById(log.getMessageId());
            systemMessageUserLogService.isRead(log);
            return entityResponse(message);
        }catch (Exception e){
            log.error("查询详情报错:{}", e);
            return null;
        }
    }
}
